<template>
  <a-card title="既往病史" :bordered="false">
    <a-spin :spinning="loading">
      <a-tabs tab-position="top" v-model="active">
        <a-tab-pane key="1" tab="低血糖发生史" class="tab-bg-color">
          <Hypoglycemia ref="anamnesis1" :anamnesis="currentPatient" @save="onSave" @confirm="confirm" />
        </a-tab-pane>

        <a-tab-pane key="2" tab="糖尿病酮症酸中毒发生史" class="tab-bg-color">
          <DiabeticKetoacidosis ref="anamnesis2" :anamnesis="currentPatient" @save="onSave" @confirm="confirm" />
        </a-tab-pane>

        <a-tab-pane key="3" tab="糖尿病高渗综合征发生史" class="tab-bg-color">
          <DiabeticHyperosmolarSyndrome
            ref="anamnesis3"
            :anamnesis="currentPatient"
            @save="onSave"
            @confirm="confirm"
          />
        </a-tab-pane>

        <a-tab-pane key="4" tab="糖尿病乳酸酸中毒发生史" class="tab-bg-color">
          <DiabeticLacticAcidosis ref="anamnesis4" :anamnesis="currentPatient" @save="onSave" @confirm="confirm" />
        </a-tab-pane>

        <a-tab-pane key="5" tab="其他原因导致肾脏疾病" class="tab-bg-color">
          <KidneyDisease ref="anamnesis5" :anamnesis="currentPatient" @save="onSave" @confirm="confirm" />
        </a-tab-pane>

        <a-tab-pane key="6" tab="其他病史" class="tab-bg-color">
          <OtherMedicalHistory ref="anamnesis6" :anamnesis="currentPatient" @save="onSave" @confirm="confirm" />
        </a-tab-pane>
      </a-tabs>
    </a-spin>
  </a-card>
</template>

<script>
import { PAGE_MENU_TYPE } from '@/store/mutation-types'
import { getAction, postAction } from '@/api/manage'
import Hypoglycemia from './anamnesis/Hypoglycemia'
import DiabeticKetoacidosis from './anamnesis/DiabeticKetoacidosis'
import DiabeticHyperosmolarSyndrome from './anamnesis/DiabeticHyperosmolarSyndrome.vue'
import DiabeticLacticAcidosis from './anamnesis/DiabeticLacticAcidosis'
import KidneyDisease from './anamnesis/KidneyDisease'
import OtherMedicalHistory from './anamnesis/OtherMedicalHistory'

export default {
  name: 'Anamnesis',
  components: {
    Hypoglycemia,
    DiabeticKetoacidosis,
    DiabeticHyperosmolarSyndrome,
    DiabeticLacticAcidosis,
    KidneyDisease,
    OtherMedicalHistory
  },
  data() {
    return {
      url: {
        getAnamnesis: '102/18/1021802',
        updateAnamnesis: '102/18/1021801',
        confirmAnamnesis: '102/10/1021803'
      },
      loading: false,
      active: '1',
      type: '1',
      currentPatient: {},
      text: '此操作代表你已完成了页面的填写，是否继续？'
    }
  },
  created() {
    this.getPatientInfo()
      .catch(error => {
        console.log(error)
      })
      .finally(_ => {
        this.loading = false
      })
  },
  methods: {
    confirm(type) {
      let patientId = this.$route.query[PAGE_MENU_TYPE.PatientId]
      let params = {
        patientId: patientId,
        isFinished: 1,
        type
      }
      postAction(this.url.confirmAnamnesis, params)
        .then(res => {
          if (res.success) {
            this.$message.success('页面已确认', 5)
            type != '6' && (this.active = JSON.stringify(+type + 1))
          }
        })
        .finally(_ => {
          this.$refs['anamnesis' + type].saveLoading = false
        })
    },
    async getPatientInfo() {
      let patientId = this.$route.query[PAGE_MENU_TYPE.PatientId]

      let url = this.url.getAnamnesis + '?patientId=' + patientId

      const res = await getAction(url)

      if (res.success) {
        this.currentPatient = await res.result
        console.log(this.currentPatient)
        console.log('成功获取患者既往病史信息')
      } else {
        console.log('获取患者既往病史信息失败，自动为该患者新建一个空档案')
        const newRecord = await postAction(this.url.updateAnamnesis, {
          patientId: patientId
        })

        if (newRecord.success) {
          this.getPatientInfo()
          console.log(newRecord)
        }
      }
    },
    onSave(type) {
      const params = JSON.parse(JSON.stringify(this.currentPatient))
      postAction(this.url.updateAnamnesis, params)
        .then(res => {
          if (res.success) {
            console.log(res)
            this.$message.success('操作成功')
            type != '6' && (this.active = JSON.stringify(+type + 1))
          } else {
            this.$message.error('操作失败')
          }
        })
        .finally(_ => {
          this.$refs['anamnesis' + type].saveLoading = false
        })
    }
  }
}
</script>

<style lang="less" scoped>
.tab-bg-color {
  padding: 15px;
}
</style>

